package com.example.elysiumease;

import com.example.elysiumease.mapper.JudgmentRecordMapper;
import com.example.elysiumease.model.JudgmentRecord;
import com.example.elysiumease.model.JudgmentRecord.JudgmentResult;
import com.example.elysiumease.model.Soul;
import com.example.elysiumease.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
@Commit
public class JudgmentRecordMapperTest {

    @Autowired
    private JudgmentRecordMapper judgmentRecordMapper;


    @Test
    void testInsertJudgmentRecord() {
        JudgmentRecord judgmentRecord = new JudgmentRecord();
        Soul soul = new Soul(); // 确保灵魂ID已存在
        soul.setSoulId(1);

        User judge = new User(); // 确保审判者ID已存在
        judge.setUserId(1);
        judgmentRecord.setSoul(soul);
        judgmentRecord.setJudge(judge);
        judgmentRecord.setFinalScore(new BigDecimal("95.00"));
        judgmentRecord.setJudgmentResult(JudgmentResult.HEAVEN);
        judgmentRecord.setJudgmentDetails("This soul has lived a good life.");
        judgmentRecord.setJudgmentTime(LocalDateTime.now());

        int result = judgmentRecordMapper.insertJudgmentRecord(judgmentRecord);
        assertEquals(1, result);
        assertNotNull(judgmentRecord.getRecordId());
    }

    @Test
    void testSelectBySoulId() {
        List<JudgmentRecord> judgmentRecords = judgmentRecordMapper.selectBySoulId(1);
        assertNotNull(judgmentRecords);
        assertFalse(judgmentRecords.isEmpty());
    }

    @Test
    void testSelectById() {
        JudgmentRecord judgmentRecord = judgmentRecordMapper.selectById(1L);
        assertNotNull(judgmentRecord);
        assertNotNull(judgmentRecord.getRecordId());
    }

    @Test
    void testUpdateJudgmentRecord() {
        JudgmentRecord judgmentRecord = judgmentRecordMapper.selectById(1L);
        assertNotNull(judgmentRecord);

        judgmentRecord.setJudgmentDetails("Updated judgment details.");
        int result = judgmentRecordMapper.updateJudgmentRecord(judgmentRecord);
        assertEquals(1, result);

        JudgmentRecord updatedJudgmentRecord = judgmentRecordMapper.selectById(1L);
        assertEquals("Updated judgment details.", updatedJudgmentRecord.getJudgmentDetails());
    }

    @Test
    void testDeleteJudgmentRecord() {
        int result = judgmentRecordMapper.deleteJudgmentRecord(1L);
        assertEquals(1, result);
        JudgmentRecord judgmentRecord = judgmentRecordMapper.selectById(1L);
        assertNull(judgmentRecord);
    }

    @Test
    void testDeleteBatchJudgmentRecord() {
        List<Long> recordIds = new ArrayList<>();
        recordIds.add(1L);
        recordIds.add(2L);

        int result = judgmentRecordMapper.deleteBatchJudgmentRecord(recordIds);
        assertEquals(2, result);

        for (Long recordId : recordIds) {
            JudgmentRecord judgmentRecord = judgmentRecordMapper.selectById(recordId);
            assertNull(judgmentRecord);
        }
    }
}